package com.runrunfast.face.controller;

import com.alibaba.fastjson.JSONObject;
import com.runrunfast.face.dao.DeviceRelationRepository;
import com.runrunfast.face.entity.Camera;
import com.runrunfast.face.entity.YouBox;
import com.runrunfast.face.service.DeviceService;
import com.runrunfast.face.service.PersonService;
import com.runrunfast.face.service.common.YoutuService;
import com.runrunfast.face.util.ToolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author YangNing
 */
@Api(description = "设备")
@RestController
public class DeviceController {

    private static Logger logger = LoggerFactory.getLogger(DeviceController.class);

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DeviceRelationRepository deviceRelationRepository;
    @Autowired
    YoutuService youtuService;
    @Autowired
    PersonService personService;
    @Autowired
    DeviceService deviceService;

    @ApiOperation("增加摄像头")
    @PostMapping("/addCamera")
    public JSONObject addCamera(@RequestBody Camera camera) {
        // TODO: 2018/5/22 新建设备时的name不能重复
        //必填参数校验
        if (ToolUtil.isOneEmpty(camera,
                camera.getName(),
                camera.getDevice_group_id(),
                camera.getBox_id(),
                camera.getPic_size(),
                camera.getPic_type(),
                camera.getTranscode_size())) {
            return null;
        }
        //普通摄像头没设置rtsp地址
        if (1 != camera.getType() && ToolUtil.isEmpty(camera.getRtsp_url())) {
            return null;
        }
        return deviceService.addCamera(camera);
    }

    @ApiOperation("增加YouBox(暂时没有上报事件触发)")
    @PostMapping("/addYouBox")
    public JSONObject addYouBox(@RequestBody YouBox youBox) {
        if (ToolUtil.isOneEmpty(youBox,
                youBox.getName(),
                youBox.getDevice_group_id(),
                youBox.getHardware_key())) {
            return null;
        }
        return deviceService.addYouBox(youBox);
    }
}
